/**
 * 
 */
package lu.med.praxiswahl.wahlumed.client.addressbook;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.model.beans.DoctorsTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.DoctorServiceInt;

/**
 * @author PatrickPierre
 * 
 */
public class AddressBookModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2253585331343070830L;
	private ArrayList<AddressBookDataRow> bookRow;
	private Main dataManager;

	private int selectedRow = -1;

	public AddressBookModel(Main dataManager) {

		this.dataManager = dataManager;
		this.bookRow = new ArrayList<AddressBookDataRow>();
	}

	@Override
	public int getColumnCount() {
		return 4;
	}

	@Override
	public int getRowCount() {

		return bookRow.size();
	}

	@Override
	public Object getValueAt(int row, int col) {
		switch (col) {
		case 0:
			return bookRow.get(row).getName();
		case 1:
			return bookRow.get(row).getAddresse();
		case 2:
			return bookRow.get(row).getTelefon();
		case 3:
			return bookRow.get(row).getFax();
		}
		return "";
	}

	@Override
	public boolean isCellEditable(int row, int col) {
		return false;
	}

	@Override
	public String getColumnName(int columnIndex) {
		switch (columnIndex) {
		case 0:
			return "Name";
		case 1:
			return "Addresse";
		case 2:
			return "Telefon";
		case 3:
			return "Fax";
		}
		return "";
	}

	public void transferFromDatabase() {

		this.bookRow.clear();
		DoctorServiceInt doc = this.dataManager.getDoctorContext();

		DoctorsTBL[] tmpDoctors = doc.retrieveAllAddress();

		for (int i = 0; i < tmpDoctors.length; i++) {
			AddressBookDataRow buffer = new AddressBookDataRow();
			buffer.setName(tmpDoctors[i].getNachname() + " "
					+ tmpDoctors[i].getVorname());
			buffer.setAddresse(tmpDoctors[i].getAdresse());
			buffer.setId(tmpDoctors[i].getId());
			buffer.setEmail(tmpDoctors[i].getEmail());
			buffer.setFax(tmpDoctors[i].getFax());
			buffer.setTelefon(tmpDoctors[i].getTelefon());
			this.bookRow.add(buffer);
		}

		this.fireTableDataChanged();
	}

	public void retrieveAddressEntry(String searchStr) {

		this.bookRow.clear();
		DoctorServiceInt doc = this.dataManager.getDoctorContext();

		DoctorsTBL[] tmpDoctors = doc.retrieveAddressEntry(searchStr);

		for (int i = 0; i < tmpDoctors.length; i++) {
			AddressBookDataRow buffer = new AddressBookDataRow();
			buffer.setName(tmpDoctors[i].getNachname() + " "
					+ tmpDoctors[i].getVorname());
			buffer.setAddresse(tmpDoctors[i].getAdresse());
			buffer.setId(tmpDoctors[i].getId());
			buffer.setEmail(tmpDoctors[i].getEmail());
			buffer.setFax(tmpDoctors[i].getFax());
			buffer.setTelefon(tmpDoctors[i].getTelefon());
			this.bookRow.add(buffer);
		}

		this.fireTableDataChanged();
	}
	
	public void clearModel(){
		
		this.bookRow.clear();
		this.fireTableDataChanged();
	}
	
	public void transferToDatabase(String matricule) {

	}

	public void deletePhysician(String file, long id) {

	}

	public int getSelectedRow() {
		return selectedRow;
	}

	public void setSelectedRow(int selectedRow) {
		this.selectedRow = selectedRow;
	}

	public ArrayList<AddressBookDataRow> getBookRow() {
		return bookRow;
	}

	public void setBookRow(ArrayList<AddressBookDataRow> bookRow) {
		this.bookRow = bookRow;
	}
	
}
